package bootstarp;

import job.HelloJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import static org.quartz.TriggerBuilder.newTrigger;

public class SimpleQuartzMainClass {
    public static void main(String[] args) throws SchedulerException {
        // 工厂方法创建调度器
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        // 创建调度任务的job
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("message","打印日志名称");
        JobDetail job = JobBuilder.newJob(HelloJob.class)
                .withIdentity("job1", "group1")
                .usingJobData(jobDataMap)
                .build();

        System.out.println("---->getDescription："+job.getDescription());
        System.out.println("---->job.getKey().getName()："+job.getKey().getName());
        System.out.println("---->job.getKey().getGroup()："+job.getKey().getGroup());
        System.out.println("---->job.getKey().getClass()："+job.getKey().getClass());


        // 创建触达条件：【注意】触达器的group和job的group不是同一个组的概念
        jobDataMap.put("message","打印日志名称222");
        Trigger trigger = newTrigger()
                .withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(3)
                        .repeatForever())
                .usingJobData(jobDataMap)
                .build();

        // 告诉调度器要执行的额job和触达条件
        scheduler.scheduleJob(job, trigger);
        // 启动任务
        scheduler.start();
        // 结束任务
//        scheduler.shutdown();
    }
}
